package com.maxd.modules.antPlant.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.gexin.fastjson.JSON;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.CommonInfo;
import com.maxd.modules.antPlant.api.AntPlanetComplexApi;
import com.maxd.modules.antPlant.api.resp.WphPageQueryResp;
import com.maxd.modules.antPlant.api.resp.RechargeOrderQueryResp;
import com.maxd.modules.antPlant.api.resp.WphSearchQueryResp;
import com.maxd.modules.antPlant.api.resp.WphTransferResp;
import com.maxd.respository.common.CommonRepository;
import com.maxd.utils.HttpClientUtil;
import com.maxd.utils.MD5Util;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author mengshx
 * datetime 2024-04-14 09:10
 **/
@RestController
@RequestMapping("antPlanet")
public class AntPlanetController {
    @Autowired
    AntPlanetComplexApi api;
    @Autowired
    private CommonRepository commonRepository;
    @GetMapping("activityCategory")
    @IgnoreSecurity
    public Result activityCategory(
    ) throws Exception{
        JsonNode resp = api.activityCategory();
        return ResultUtil.success(resp);
    }
    @GetMapping("activityList")
    @IgnoreSecurity
    public Result activityList(
            Pageable page,
            @RequestParam(value = "actid",required = false) Integer actid,
            @RequestParam(value = "cid",required = false) Integer cid,
            @RequestParam(value = "keyword",required = false) String keyword
    ) throws Exception{
        JsonNode resp = api.activityPage(page, actid, cid, keyword);
        return ResultUtil.success(resp);
    }


    @GetMapping("activityShare")
    @IgnoreSecurity
    public Result activityShare(
            @RequestParam("actid") Integer actid,
            @RequestParam("relationId") String relationId
    ) throws Exception{
        JsonNode resp = api.activityShare(actid,relationId);
        return ResultUtil.success(resp);
    }
    @GetMapping("queryOrder")
    @IgnoreSecurity
    public Result activityShare(
            Pageable page,
            Date startTime,
            Date endTime,
            String orderId
    ) throws Exception{
        RechargeOrderQueryResp resp = api.queryRechargeOrder(page.getPageNumber(),page.getPageSize(),
                startTime,endTime,
                null,
                orderId,
                null,
                null
                );
        return ResultUtil.success(resp);
    }


    @GetMapping("/getWphPage")
    @ApiOperation("蚂蚁星球链接转链")
    @IgnoreSecurity
    public Result getWphList(String relationId,String adsId){
        CommonInfo clientId = commonRepository.findOne(201);
        CommonInfo clientSecret = commonRepository.findOne(202);
        StringBuilder url=new StringBuilder("https://open.duomai.com/apis?");
        String str="{\"ads_id\":\""+adsId+"\",\"site_id\":\""+clientId.getValue()+"\",\"ext\":{\"euid\":\""+relationId+"\"}}";
        String timestamp=System.currentTimeMillis()/1000+"";
        String sign = clientSecret.getValue() + "app_key" + clientId.getValue() + "servicecps-mesh.cpslink.links.post" +
                "timestamp" + timestamp + str + clientSecret.getValue();
        String s = MD5Util.md5Encrypt32Upper(sign);
        url.append("app_key=").append(clientId.getValue()).append("&sign=").append(s).append("&service=cps-mesh.cpslink.links.post");
        url.append("&timestamp=").append(timestamp);
        String s1 = HttpClientUtil.doPostJson(url.toString(), str);
        return ResultUtil.success(JSON.parseObject(s1));
    }
    @GetMapping("/searchWphPage")
    @ApiOperation("蚂蚁星球唯品会搜索列表")
    @IgnoreSecurity
    public Result getWphList(
            Pageable page,
            @RequestParam(value = "keyword",required = false) String keyword
    ){
        WphSearchQueryResp resp = api.searchWph(page.getPageNumber(), page.getPageSize(), keyword);
        return ResultUtil.success(resp);
    }
    @GetMapping("/getWphUrl")
    @ApiOperation("蚂蚁星球唯品会转链")
    @IgnoreSecurity
    public Result getAntUrl(String relationId,String goodsId){
        WphTransferResp resp = api.transferWph(relationId,goodsId);
        return ResultUtil.success(resp);
    }
}
